var fs = require("fs");
var _ = require("lodash");

let fileNameB = "../extra";
let fileNameC = "./data/character3";
let fileNameD = "./CvLines";
let fileNameE = "./DailyCvLines";
let fileNameF = "./GachaCvLines";
let outputFileName = "./lua/characterStory.lua";

let extraData = JSON.parse(fs.readFileSync(`${fileNameB}.json`).toString()).character;
let cvData = JSON.parse(fs.readFileSync(`${fileNameD}.json`).toString()).map;
let dcvData = JSON.parse(fs.readFileSync(`${fileNameE}.json`).toString()).map;
let gcvData = JSON.parse(fs.readFileSync(`${fileNameF}.json`).toString()).map;
let mainData = JSON.parse(fs.readFileSync(`${fileNameC}.json`).toString());

let extraMap = new Map();
let cvMap = new Map();
let dcvMap = new Map();
let gcvMap = new Map();
_.forEach(extraData, (e) => {
    let id = e.id;
    let data = {
        img: e.img,
        add_date: e.add_date,
        zx: e.zx
    };
    extraMap.set(id, data);
});
_.forEach(cvData, (e) => {
    let id = Math.floor(e.id / 1000);
    if (!cvMap.has(id)) cvMap.set(id, Array());
    let l = cvMap.get(id);
    l.push(e.data);
    cvMap.set(id, l);
});
_.forEach(dcvData, (e) => {
    let id = Math.floor(e.id / 1000);
    if (!dcvMap.has(id)) dcvMap.set(id, Array());
    let l = dcvMap.get(id);
    l.push(e.data);
    dcvMap.set(id, l);
});
_.forEach(gcvData, (e) => {
    let id = Math.floor(e.id / 1000);
    gcvMap.set(id, e.data);
});

function addCnData(cnData) {
    if (!cnData) return "''";
    let str = "{\n";
    let i = 1;
    for(d of cnData) {
        str += `[${i++}] = [[${d}]],\n`;
    }
    str += "}"
    return str;
}

function addCv(id, type) {
    let map = type == 0 ? cvMap : dcvMap;
    if (!map.has(id)) return "''";
    arr = map.get(id);
    let str = "{\n";
    let i = 1;
    for(d of arr) {
        str += `[${i++}] = {
        ['id'] = '${d.id}',
        ['name'] = '${d.name}',
        ['des'] = [[${d.description}]],
        },
    `;
    }
    str += "}"
    return str;
}

function addGcv(id) {
    if (!gcvMap.has(id)) return "''";
    arr = gcvMap.get(id);
    let str = `{
        ['id'] = '${arr.id}',
        ['name'] = '${arr.name}',
        ['des'] = '${arr.description}',
    }`
    return str;
}

let output = `local p = {}
`

_.forEach(mainData, (e) => {
    let data = e.data;
    let exData = extraMap.has(e.id) ? extraMap.get(e.id) : {zx:{}};
    let str = `p['${e.id}'] = {
    ['name'] = '${data.name}',
    ['race'] = '${data.race}',
    ['type'] = '${data.characterType_}',
    ['age'] = '${data.age}',
    ['height'] = '${data.height}',
    ['birth'] = '${data.birth}',
    ['gender'] = '${data.gender}',
    ['favourite'] = '${data.favourite}',
    ['jpVoice'] = '${data.jpVoice}',
    ['designer'] = '${data.designer}',
    ['prefix'] = '${data.prefix}',
    ['enName'] = '${data.enName}',
    ['addDate'] = ${exData.add_date || "''"},
    ['cnData'] = ${addCnData(data.cnData)},
    ['cv'] = ${addCv(e.id, 0)},
    ['dailyCv'] = ${addCv(e.id, 1)},
    ['gcv'] = ${addGcv(e.id)},
}
`;
    output += str;
});
output += `return p`

fs.writeFileSync(outputFileName, output);
